<?php

class category {

    /**
     *   @desc Variable Declaration with default value
     */
    private $_id;   // KEY ATTR. WITH AUTOINCREMENT
    private $_title;		private $_alias; ###
	private $_parentid;
	private $_description;
    private $_sortorder;
	private $_addeddate;
	private $_updateddate;
    private $_status;
	

    /**
     *   @desc   CONSTRUCTOR METHOD
     */
    function __construct() {
        global $DBOBJ;
        $this->_obj = $DBOBJ;
        $this->_id = null;
        $this->_title = null;				$this->_alias = null; ###
		$this->_parentid = null;
		$this->_description = null;
        $this->_sortorder = null;
		$this->_addeddate = date('Y-m-d h:i:s');
		$this->_updateddate = date('Y-m-d h:i:s');
        $this->_status = null;
		
    }

    /**
     *   @desc   DECONSTRUCTOR METHOD
     */
    function __destruct() {
        unset($this->_obj);
    }

    /**
     *   @desc   GETTER METHODS
     */
    public function getid() {
        return $this->_id;
    }

    public function gettitle() {
        return $this->_title;
    }
		#####	public function getalias() {        return $this->_alias;    }	
	public function getparentid() {
        return $this->_parentid;
    }
	
	
	public function getdescription() {
        return $this->_description;
    }
	
    public function getsortorder() {
        return $this->_sortorder;
    }
	
	public function getaddeddate() {
	return $this->_addeddate;
    }
	
	
	public function getupdateddate() {
	return $this->_updateddate;
    }

    public function getstatus() {
        return $this->_status;
    }

    /**
     *   @desc   SETTER METHODS
     */
    public function setid($id) {
        $this->_id = $id;
    }

    public function settitle($title) {
        $this->_title = $title;
    }
		###	public function setalias($alias) {        $this->_alias = $alias;    }	
	public function setparentid($parentid) {
        $this->_parentid = $parentid;
    }
	
	public function setdescription($description) {
        $this->_description = $description;
    }
	
    public function setsortorder($sortorder) {
        $this->_sortorder = $sortorder;
    }
	
	public function setaddeddate($addeddate) {
	$this->_addeddate = $addeddate;
    }
	
	public function setupdateddate($updateddate) {
	$this->_updateddate = $updateddate;
    }

    public function setstatus($status) {
        $this->_status = $status;
    }

    /**
     *   @desc   SELECT METHOD / LOAD
     */
    public function select($id = '') {
        $sql =  "SELECT * FROM `category` WHERE `id` = '". $id ."'";
        $row = $this->_obj->select($sql);

        if (count($row) > 0) {
            $this->_id = $row[0]['id'];
            $this->_title = $row[0]['title'];			$this->_alias = $row[0]['alias'];  ###
			$this->_parentid = $row[0]['parentid'];
			$this->_description = $row[0]['description'];
            $this->_sortorder = $row[0]['sortorder'];
			$this->_addeddate = $row[0]['addeddate'];
			$this->_updateddate = $row[0]['updateddate'];
            $this->_status = $row[0]['status'];
            return $row[0];
        } else {
            return;
        }
    }

    /**
     *   @desc   set All
     */
    public function setAll($dataArray = array()) {
        if (count($dataArray) <= 0) {
            return;
        }

        foreach ($dataArray as $key => $value) {
            $classKey = '_' . $key;
            $this->$classKey = $value;
        }
    }

    /**
     *   @desc   get All
     */
    public function getAll($FORMAT = 'ARRAY') {
        $dataArray = new stdClass();
        $dataArray->id = $this->getid();
        $dataArray->title = $this->gettitle();		$dataArray->alias = $this->getalias();  ###
		$dataArray->parentid = $this->getparentid();
		$dataArray->description = $this->getdescription();
        $dataArray->sortorder = $this->getsortorder();
		$dataArray->addeddate = $this->getaddeddate();
		$dataArray->updateddate = $this->getupdateddate();
        $dataArray->status = $this->getstatus();
        if ($FORMAT == 'ARRAY') {
            return (array) $dataArray;
        }
        return $dataArray;
    }

    /**
     *   @desc   COUNT
     */
    public function getCountcategory() {
        $sql = "SELECT count(*) as cnt  FROM `category`";
        $result = $this->_obj->select($sql);
        return $result['0']['cnt'];
    }

    /**
     *   @desc   DELETE
     */
    public function delete($id) {
        $sql = "DELETE FROM `category` WHERE `id` = '".$id."'";
        $result = $this->_obj->sql_query($sql);
    }

    /**
     *   @desc   INSERT
     */	 	 	     public function insert() {        $this->id = ""; // clear key for autoincrement        $sql = "INSERT INTO `category` ( `id`,`title`,`alias`,`parentid`,`description`,`sortorder`,`addeddate`,`updateddate`,`status` ) VALUES                ( NULL,                '" . $this->_title . "',				'" . $this->_alias . "',								'" . $this->_parentid . "',				'" . $this->_description . "',                '" . $this->_sortorder . "',				'" . $this->_addeddate . "',				'" . $this->_updateddate . "',                '" . $this->_status . "' )";        $result = $this->_obj->insert($sql);        return $result;    }    /**     *   @desc   UPDATE     */    public function update($id) {        $sql = " UPDATE category SET                    `id` = '" . $this->_id . "' ,                    `title` = '" . $this->_title . "' ,					`alias` = '" . $this->_alias . "' ,					`parentid` = '" . $this->_parentid . "' ,					`description` = '" . $this->_description . "' ,                    `sortorder` = '" . $this->_sortorder . "' ,					`addeddate` = '" . $this->_addeddate . "',					`updateddate` = '" . $this->_updateddate . "',                    `status` = '" . $this->_status . "'                WHERE `id` = '".$id."'";        $result = $this->_obj->sql_query($sql);    }	 
	
    public function checkCategoryExists($title) {
        $sql = "SELECT * FROM `category` WHERE `title` = '".$title."' limit 1";
        $row = $this->_obj->select($sql);
        if (count($row) > 0) {
            return true;
        } else {
            return false;
        }
    }
		#####	public function checkCategoryAliasExists($alias) {        $sql = "SELECT * FROM `category` WHERE `alias` = '".$alias."' limit 1";        $row = $this->_obj->select($sql);        if (count($row) > 0) {            return true;        } else {            return false;        }    }		####	//In the Edit mode 	public function checkCategoryAlias($currentAlias = '' ,$alias) {        $sql = "select * from category where alias = '" . $currentAlias . "' or alias = '".$alias."' ";        $row = $this->_obj->select($sql);        if (count($row) > 1) {            return false;        }        return true;    }		
    public function getAllCategoryList($params = '*', $where = '', $orderby = "id", $limit = '') {
        $orderby = 'ORDER BY ' . $orderby;
        $sql = "SELECT " . $params . " FROM `category` c  " . $where . " " . $orderby . " " . $limit;
        $row = $this->_obj->select($sql);
        if ($row) {
            return $row;
        }
        return array();
    }

    function performListAction($action = '', $ids = "''") {
        switch ($action) {
            case 'delete':
                $sql = "DELETE FROM `category` WHERE `id` IN (" . $ids . ")";
                break;
            default:
                $sql = '';
        }
        if ($sql == '') {
            return false;
        }
        $result = $this->_obj->sql_query($sql);
        return true;
    }
	
	public function checkCategoryExistInBrand($categoryId = '') {
		//$sql = "SELECT COUNT(cid) AS 'cnt' FROM product WHERE cid= '".$categoryId."'";		$sql = "select count(*) as 'cnt' from brand_to_category where cid = '".$categoryId."'";						
		$result = $this->_obj->select($sql);
		return $result['0']['cnt'];
	}
	
	public function categoryDelete($dataArray = Array()) {
		$categoryId = $dataArray['categoryid'];
		$count = $this->checkChildCategoryExist($categoryId);
		$countProduct = $this->checkCategoryExistInBrand($categoryId);  
		if ( $count >0 ) {
			echo 'UNSUCCESS';
		} else if ( $countProduct > 0 ) {
			echo 'UNSUCESSPRODUCTEXIST';
		} else {
			$this->performListAction('delete', $categoryId);			$deleteSql = "DELETE FROM `brand_to_category` WHERE `cid` = '".$categoryId."'";			$result = $this->_obj->sql_query($deleteSql);
		}
		exit;
	}
	
	
	public function checkChildCategoryExist($categoryId = '') {
		$sql = "select count(*) as 'cnt' from category where parentId = '".$categoryId."'";
		$row = $this->_obj->select($sql);
		return $row['0']['cnt'];
	}

    public function getCategoryListById($where = '') {
        if ($where == '') {
            $where = "WHERE `status` = '1'";
        } 
        $sql = "SELECT `id`,`title` FROM `category` ".$where."";
        $row = $this->_obj->select($sql);
        $returnArray = Array();
        if (count($row) > 0) {
            foreach($row as $value) {
            $returnArray[$value['id']] =  $value['title'];
            }
        }
        return $returnArray;
    }

}

?>